import { OrthographicCamera, PerspectiveCamera } from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';


class ArcRotateControls extends OrbitControls {

    protected _scale!: number;
    protected _dollyTotal = 1;
    protected _tempScale = 1;

    public dampingDollyRatio = 0.4;
    public dampingZoomRatio = 2;

    protected _dollyOut(dollyScale: number): void {

        if ((this.object as PerspectiveCamera).isPerspectiveCamera || (this.object as OrthographicCamera).isOrthographicCamera) {
            this._dollyTotal /= this.setDollyScale(dollyScale);

        } else {

            console.warn('WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.');
            this.enableZoom = false;

        }

    }

    protected _dollyIn(dollyScale: number): void {

        if ((this.object as PerspectiveCamera).isPerspectiveCamera || (this.object as OrthographicCamera).isOrthographicCamera) {
            this._dollyTotal *= this.setDollyScale(dollyScale);

        } else {

            console.warn('WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.');
            this.enableZoom = false;

        }

    }

    private setDollyScale(value: number): number {
        return this.enableDamping ? 1 + (value - 1) * this.dampingDollyRatio : value;
    }

    private dampingZoom(): void {
        if (!this._dollyTotal || Math.abs(this._dollyTotal - 1) < 1e-6) return;
        if (!this.enableDamping) {
            this._scale = this._dollyTotal;
            this._dollyTotal = 1;
            return;
        }
        this._dollyTotal = 1 + (this._dollyTotal - 1) * (1 - this.dampingFactor * this.dampingZoomRatio);
        this._scale = this._dollyTotal;
    }

    public update(deltaTime?: number | null): boolean {
        this.dampingZoom();
        return super.update(deltaTime);
    }

}


export { ArcRotateControls };